<script type="text/javascript">
   //当前菜单节点
   var currentMenuNode;
   var isLeaf;
   function loadMenuInfo(node) {
	   var pnode = $('#sysMenuTree').tree('getParent',node.target); 
 		console.log(pnode);
 		currentMenuNode = node;
		console.log(node);
		var pMenuId = '';
	    var pMenuName= '';
	    if (pnode) {
		   pMenuId = pnode.id;
		   pMenuName = pnode.text;
	    }else {
		   
	    }
		var menu = {
			pMenuId:pMenuId,
			pMenuName:pMenuName,
			id:node.id
		};
		console.log(menu);
		
	    $.post('menuFunction.do?loadMenuInfo', {
			data : JSON.stringify(menu)
		},
		function(result) {
			console.log(result);
			if (result) {
				result.iframe = ''+result.iframe;
				result.enabled = ''+result.enabled;
				if(result.pMenuId==null) {
					result.pMenuId = '';
				}
				$("#menuForm").form('load', result);
				printLog(result.pMenuId);
				if(result.pMenuId==null) {
					$('#selectMenuFunTree').combotree('setValue','');
				}else {
					$('#selectMenuFunTree').combotree('setValue',result.pMenuId+'');
				}
				
				
// 				$('#iframeComboBox').combobox(
// 						'reload',
// 						'common.do?loadComboBox&key=YON&value='
// 								+ result.iframe);
// 				$('#enabledComboBox').combobox(
// 						'reload',
// 						'common.do?loadComboBox&key=YON&value='
// 								+ result.enabled);
			} else {
				showMsg('加载失败.');
			}
		}, 'json');
	    
	    
		isLeaf = $('#sysMenuTree').tree('isLeaf', node.target);
		printLog('是否根节点' + isLeaf);
		//加载按钮列表
		loadButtonList(node.id);
	}
	//加载按钮列表
	function loadButtonList(menuId) {
		if (isLeaf) {
			var params = $('#buttonlist').datagrid('options').queryParams; //先取得 datagrid 的查询参数  
			printLog(params);
			if (menuId) {
				params['menuId'] = menuId;
			}

			printLog(params);
			$('#buttonlist').datagrid('reload'); //设置好查询参数 reload 一下就可以了  
		} else {

		}

	}

	function addMenuTreeItem() {
		$('#menuForm').form('clear');
		var node = $("#sysMenuTree").tree("getSelected");
		console.log(node);
		if (node) {
			// 		   var pnode = $('#sysMenuTree').tree('getParent',node.target); 
			var pMenuId = '';
			// 		   var pMenuName= '';
			// 		   if (pnode) {
			var pMenuId = node.id;
			var pMenuName = node.text;
			// 		   }else {

			// 		   }
			var menu = {
				pMenuId : pMenuId,
				pMenuName : pMenuName,
				nameCN : '',
				pageUrl : '',
				iconName : ''
			};
			printLog(menu)
			$("#menuForm").form('load', menu);
		} else {
			// 		   showMsg('请选择菜单项.');
			var menu = {
				pMenuId : '',
				pMenuName : '',
				nameCN : '',
				pageUrl : '',
				iconName : ''
			};
			printLog(menu)
			$("#menuForm").form('load', menu);
		}
	}

	function removeMenu() {
		var selectNode = $('#sysMenuTree').tree('getSelected');
		printLog(selectNode);
		if (selectNode) {
			$.messager.confirm('询问', '你确认要删除菜单吗?如果菜单下关联子菜单也会自动删除', function(r) {
				if (r) {
					$.post('menuFunction.do?deleteMenuById', {
						menuId : selectNode.id
					}, function(data) {
						if (data.success) {
							$('#sysMenuTree').tree('reload');
						}
						showMsg(data.msg);
					}, 'json');
				}
			});
		} else {
			showMsg('请选择左侧操作菜单');
		}

	}

	function saveMenu() {
		var actionurl = $('#menuForm').attr('action');//提交路径
		// 		var formData = {};//$('#menuForm').serialize();
		// 	    printLog($('#menuForm').serialize());
		// 		$("#menuForm :input").each(function() {
		// 			 formData[this.name] =$("#"+this.name ).val();
		// 		});

		// 		printLog(formData);
// 		printLog(actionurl);
		printLog($('#menuForm').form("getData"));
		$.ajax({
			async : false,
			cache : false,
			type : 'POST',
			url : actionurl,// 请求的action路径
			data : $('#menuForm').form("getData"),
			error : function() {// 请求失败处理函数
				showMsg('保存失败.');
			},
			success : function(data) {
				printLog(data);
				$('#sysMenuTree').tree('reload');
				showMsg('保存成功.');
			}
		});
	}
	var editIndex2 = undefined;//标示当前编辑行，提高效率，避免使用each遍历所有行来关闭正在编辑的行。
	function endEditing() {
		if (editIndex2 == undefined) {
			return true;
		}
		if ($('#buttonlist').datagrid('validateRow', editIndex2)) {
			var ed = $('#buttonlist').datagrid('getEditor', {
				index : editIndex2,
				field : 'operationCode'
			});
			$('#buttonlist').datagrid('endEdit', editIndex2);
			editIndex2 = undefined;
			return true;
		} else {
			return false;
		}
	}

	function buttonlist_onClickRow(index, data) {
		if (editIndex2 != index) {
			if (endEditing()) {
				$('#buttonlist').datagrid('selectRow', index).datagrid(
						'beginEdit', index);
				editIndex2 = index;
			} else {
				$('#buttonlist').datagrid('selectRow', editIndex2);
			}
		}
	}

	function buttonlist_append() {
		if (endEditing()) {
			$('#buttonlist').datagrid('appendRow', {
				menuFunId : currentMenuNode.id,
				type : 1
			});
			editIndex2 = $('#buttonlist').datagrid('getRows').length - 1;
			$('#buttonlist').datagrid('selectRow', editIndex2).datagrid(
					'beginEdit', editIndex2);
		}
	}

	function buttonlist_remove() {
		var rows = buttonlist.datagrid('getChecked');
		printLog(rows);
		if (rows && rows.length > 0) {
			$.messager.confirm('询问', '你确认要删除记录吗?',
					function(r) {
						if (r) {
							for ( var i = rows.length - 1; i >= 0; i--) {
								printLog(rows[i]);
								var index = buttonlist.datagrid('getRowIndex',
										rows[i]);
								buttonlist.datagrid('deleteRow', index);
							}
						}
					});
		} else {
			showMsg("请选择操作记录.");
		}
	}

	function buttonlist_save() {
		if (endEditing()) {
			var inserted = $('#buttonlist').datagrid('getChanges', "inserted");
			var deleted = $('#buttonlist').datagrid('getChanges', "deleted");
			var updated = $('#buttonlist').datagrid('getChanges', "updated");
			var effectRow = {
				inserted : inserted,
				deleted : deleted,
				updated : updated
			};
			printLog(effectRow);
			printLog(JSON.stringify(effectRow));
			$.post("operation.do?saveOrUpdate", {
				data : JSON.stringify(effectRow)
			}, function(result) {
				if (result.success) {
					showMsg('保存成功.');
					$("#buttonlist").datagrid('acceptChanges');
				} else {
					showMsg('保存失败.');
				}
			}, "json");
		}
	}

	function showIcons() {
		createFromWindowOptions({
			title : "图标选择",
			maximizable : true,
			href : "icon.do?iconWin&selectIconName=" + $('#iconName').val(),
			callback : function(data) {
				selectIconOk('iconName');
			},
			enableSaveButton : true
		});
	}
	
	//刷新用户组
	function menuEnabledOrDisable() {
		var selectNode = $('#sysMenuTree').tree('getSelected');
		if (selectNode) {
			printLog(selectNode);
			var s = selectNode.attributes==true?"禁用":"启用";
			$.messager.confirm('询问', '你确认要'+s+'菜单吗?如果菜单下关联子菜单也会自动'+s, function(r) {
				if (r) {
					$.post('menuFunction.do?enabledOrDisabledMenuById', {
						menuId : selectNode.id
					}, function(data) {
						if (data.success) {
							$('#sysMenuTree').tree('reload');
						}
						showMsg(data.msg);
					}, 'json');
				}
			});
		} else {
			showMsg('请选择操作菜单');
		}
	}
	
	
	function formatterMenuTree(node) {
		var s = node.text;
		if(node.attributes)	{
			s = '<span style=\'color:green;\'>'+s+'</span>';
		}else {
			s = '<span style=\'color:red;\'>'+s+'</span>';
		}
		return s;
	}
</script>
<div class="easyui-layout" data-options="fit:true,border:false">
	 <div data-options="region:'west',split:true,title:'功能菜单',collapsible:false,border:true,tools:[
					{
						iconCls:'icon-ok',
						handler:function(){menuEnabledOrDisable()}
					}]" style="width:200px;">
	  <@eu.tree id="sysMenuTree" method="post" 
			url="menuFunction.do?menuFuncTree" formatter="return formatterMenuTree(node)"
			onClick="loadMenuInfo(node)"/>
	 </div>
	 
	 <div data-options="region:'center',border:true">
       <div class="easyui-layout" data-options="fit:true">
       		<div data-options="region:'north',border:false" style="height:82px;overflow: hidden;background:#F4F4F4">
       			<div  style="border-bottom:1px solid #ddd;">
			        <a href="#" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-add'" onclick="javascript:addMenuTreeItem()">新增</a>
			        <a href="#" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-remove'" onclick="javascript:removeMenu()">删除</a>
			    	<a href="#" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-save'" onclick="javascript:saveMenu()">保存</a>
			    </div>
			    <form id="menuForm" action="menuFunction.do?saveMenuInfo">
<!-- 			    	<input type="hidden" name="pMenuId"/> -->
			    	<input type="hidden" name="id"/>
			    	<table>
			    		<tr>
			    			<td class="tbLabel">父菜单:</td>
			    			<td>
			    				<@eu.comboTree id="selectMenuFunTree" required="false" url="menuFunction.do?menuFuncTree" name="pMenuId" multiple="false"/>			    			
			    			</td>
			    			<td class="tbLabel">菜单编码:</td>
			    			<td><input type="text" class="easyui-validatebox textbox" id="operationCode" name="operationCode" ></input></td>
			    			<td class="tbLabel">菜单名称:</td>
			    			<td><input type="text" class="easyui-validatebox textbox" id="nameCN" name="nameCN" ></input></td>
			    			<td>是否启用:</td>
			    			<td>
			    				<@eu.comboBox url="common.do?loadComboBox&key=YON" id="enabledComboBox" name="enabled"/>
			    			</td>
			    		</tr>
			    		<tr>
			    			<td>菜单页面:</td>
			    			<td><input type="text" class="easyui-validatebox textbox" id="pageUrl" name="pageUrl" ></input></td>
			    			<td>是否IFRAME:</td>
			    			<td>
			    				<@eu.comboBox url="common.do?loadComboBox&key=YON" id="iframeComboBox" name="iframe"/>
			    			</td>
			    			
			    			<td>菜单图标:</td>
			    			<td><input type="text" class="easyui-validatebox textbox" id="iconName" name="iconName" readonly="readonly"/><span class="searchbox-button" style="margin-top:2px;" onclick="showIcons();" title="浏览图标"></span></td>
							
			    		</tr>
			    	</table>
			    </form>
       		</div>
       		<div data-options="region:'center',border:false">
		        <@eu.datagrid id="buttonlist" idField="id" title="按钮列表" idField="id" autoEditing="true" singleEditing="true" extEditing="true"
					toolbar="#buttonlist_toolbar" pagination="false" 
					url="operation.do?loadByMenuId" item="com.xdtech.sys.vo.OperationItem" />
		        <div id="buttonlist_toolbar" style="height: auto">
		        	<@eu.linkbutton showName="新增" shiro="sys:operation:add" iconCls="icon-add" onclick="buttonlist_append()"/>
					<@eu.linkbutton showName="删除" shiro="sys:operation:delete" iconCls="icon-remove" onclick="buttonlist_remove()"/>
					<@eu.linkbutton showName="保存" shiro="sys:operation:save" iconCls="icon-save" onclick="buttonlist_save()"/>
		        </div>
       		</div>
       </div>
     </div>
</div>
